from PySide6.QtWidgets import QGroupBox, QVBoxLayout, QListWidget, QListWidgetItem, QWidget, QHBoxLayout, QCheckBox, QLabel, QPushButton


class FileListWidget(QGroupBox):
    def __init__(self, file_list_data):
        super().__init__("文件列表")

        # 设置文件列表
        self.file_list_data = file_list_data

        # 创建主布局
        self.file_layout = QVBoxLayout()
        self.file_layout.setSpacing(10)
        self.setLayout(self.file_layout)

        # 初始化文件列表
        self._init_file_list()

        # 初始化选择标签
        self._init_select_label()

        # 初始化按钮
        self._init_buttons()

    def _init_file_list(self):
        # 创建列表控件
        self.file_list = QListWidget()
        self.file_list.setSelectionMode(QListWidget.ExtendedSelection)
        self.file_layout.addWidget(self.file_list)

        # 添加示例文件
        self.add_sample_files()

    def add_sample_files(self):
        self.file_list_data = [
            ("24、架构...践.pdf", True),
        ]

        for file_name, checked in self.file_list_data:
            item = QListWidgetItem()
            widget = QWidget()
            layout = QHBoxLayout(widget)
            layout.setContentsMargins(5, 5, 5, 5)

            checkbox = QCheckBox()
            checkbox.setChecked(checked)
            layout.addWidget(checkbox)

            label = QLabel(file_name)
            layout.addWidget(label)

            layout.addStretch(1)

            item.setSizeHint(widget.sizeHint())
            self.file_list.addItem(item)
            self.file_list.setItemWidget(item, widget)

    def _init_select_label(self):
        # 全选标签
        self.select_all_label = QLabel("全选 (1/1)")
        self.file_layout.addWidget(self.select_all_label)

    def _init_buttons(self):
        # 按钮组
        self.button_layout = QHBoxLayout()
        self.button_layout.setSpacing(10)

        self.add_button = QPushButton("添加文件")
        self.clear_button = QPushButton("清空列表")

        self.button_layout.addWidget(self.add_button)
        self.button_layout.addWidget(self.clear_button)

        self.file_layout.addLayout(self.button_layout)
